package com.zhong.gang.applib.rx;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import io.reactivex.Observable;
import io.reactivex.subjects.ReplaySubject;
import io.reactivex.subjects.Subject;

/**
 * Created by ajin on 2018/3/19.
 */
public class RxBus {
    private static RxBus Bus;
    private ConcurrentHashMap<Object,Object> HashMap = new ConcurrentHashMap();

    public static RxBus get() {
        if(Bus == null) {
            Class var0 = RxBus.class;
            synchronized(RxBus.class) {
                if(Bus == null) {
                    Bus = new RxBus();
                }
            }
        }
        return Bus;
    }

    private RxBus() {}
    public <T> Observable<T> register(Object var1) {
        Object var2 = this.HashMap.get(var1);
        if(null == var2) {
            var2 = new ArrayList();
            this.HashMap.put(var1, var2);
        }
        ReplaySubject var3;
        ((List)var2).add(var3 = ReplaySubject.create());
        return var3;
    }

    public void unregister(Object var1, Observable var2) {
        List var3 = (List)this.HashMap.get(var1);
        if(null != var3) {
            if(var2 != null && var3.contains(var2)) {
                var3.remove(var2);
            }
            if(this.a(var3)) {
                this.HashMap.remove(var1);
            }
        }
    }
    public void post(Object var1) {
        this.post(var1, "");
    }
    public void post(Object var1, Object var2) {
        List var3 = (List)this.HashMap.get(var1);
        if(!this.a(var3)) {
            Iterator var4 = var3.iterator();
            while(var4.hasNext()) {
                Subject var5 = (Subject)var4.next();
                var5.onNext(var2);
            }
        }
    }
    private boolean a(Collection var1) {
        return null == var1 || var1.isEmpty();
    }
}
